<!-- 排班管理 -->
<template>
  <div class="dashboard-container transport-task customer-list-box">
    <el-form
      ref="form"
      :inline="true"
      :model="formInline"
      class="search-form"
    >
      <el-form-item
        label="员工账号："
        prop="employeeNumber"
      >
        <el-input
          v-model="formInline.employeeNumber"
          clearable
          placeholder="请输入员工账号"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="员工姓名："
        prop="name"
      >
        <el-input
          v-model="formInline.name"
          clearable
          placeholder="请输入员工姓名"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="工作模式："
        prop="workPattern"
      >
        <el-select
          v-model="formInline.workPattern"
          placeholder="请选择工作模式"
        >
          <el-option
            v-for="ele in workPatternList"
            :key="ele.id"
            v-model="ele.name"
            :label="ele.name"
            @change="selectChange(ele.name)"
          ></el-option>
        </el-select>
      </el-form-item>
      <br />
      <el-form-item
        label="月份："
        prop="month"
      >
        <el-date-picker
          v-model="formInline.month"
          type="month"
          placeholder="选择月"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item
        class="workGroupItem"
        label="所属机构："
        :popper-append-to-body="true"
        prop="organization"
      >
        <el-input
          v-model="formInline.organization"
          clearable
          placeholder="请选择所属机构"
          :suffix-icon="iconArrow"
          @click.native="showTreeFn"
        >
        </el-input>
        <el-tree
          v-if="showTree"

          class="elTree"
          :data="dataTree"
          :props="{ label: 'name' }"
          style="font-weight: 700"
          @node-click="handleNodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="onSubmit"
        >搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div
      v-loading="loading"
      class="table"
    >
      <el-button
        type="primary"
        class="setting"
        @click="scheduleSetting"
      >排班设置</el-button>
      <el-button
        class="binding"
        @click="bindingScheduling"
      >绑定排班</el-button>
      <!-- 表格 -->
      <el-table
        :data="tableData"
        style="width: 100%"
        class="table-list"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          fixed
          type="selection"
          width="55"
          height="55"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="employeeNumber"
          label="员工账号"
          width="100"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="name"
          label="员工姓名"
          width="100"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="phone"
          label="员工电话"
          width="120"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="userType"
          label="员工角色"
          width="120"
          :formatter="formatter"
        >
        </el-table-column>
        <el-table-column
          fixed
          prop="workPatternName"
          label="当前工作模式"
          width="120"
        >
        </el-table-column>
        <el-table-column
          v-for="(ele, i) in tableData.length ? tableData[0].workSchedules : []"
          :key="i"
          :label="String(i + 1)"
          width="75"
        >
          <template slot-scope="scope">
            <span
              v-if="scope.row.workSchedules[i]"
              class="workSchedules-shang"
            >上</span>
            <span
              v-else
              class="workSchedules-xiu"
            >休</span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        :current-page="formInline.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="formInline.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="prevNextClick(-1)"
        @next-click="prevNextClick(1)"
      >
      </el-pagination>
    </div>

    <!-- 模态框 -->
    <el-dialog
      title="title"
      :visible="dialogVisible"
      width="38%"
      @close="dialogVisible = false"
    >
      <div
        class="son"
        style="margin: 10px 50px"
      >
        <el-radio-group
          v-model="radio"
          @input="changeRadio"
        >
          <el-radio
            label="1"
          >礼拜制</el-radio>

          <el-radio
            label="2"
          >连续制</el-radio>
        </el-radio-group>

      </div>
      <div
        class="select"
        style="margin: 30px 0 0 60px"
      >
        模式名称:
        <el-select
          v-model="workPattern"
          placeholder="请选择"
          style="width: 300px"
          @change="getValue"
        >
          <el-option
            v-for="ele in workPatternListRadio"
            :key="ele.id"
            :label="ele.name"
            :value="ele.id"
          >
          </el-option>
        </el-select>

      </div>

      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>

        <el-button
          type="primary"
          @click="submit"
        >确 定</el-button>
      </span>

    </el-dialog>
  </div>
</template>
<script>
import { getlistApi, businessTreeApi, workPatternsApi, workSchedulingsApi } from '@/api/workarrange'
import userType from '@/constant/userType'
export default {
  data() {
    return {
      loading: false,
      formInline: {
        agencyId: '',
        employeeNumber: '',
        month: '',
        name: '', // 员工名称
        page: 1, // 页数
        pageSize: 10,
        organization: '',
        userType: '',
        workPatternId: '',

        workPattern: ''
      },
      workPatternList: [],
      showTree: false,
      iconArrow: 'el-icon-arrow-down',
      tableData: [],
      pages: 0,
      counts: 0,
      dataTree: [],
      // 模态框
      dialogVisible: false,
      radio: '1',
      workPattern: '',
      workPatternListRadio: [],
      handleSelection: [],
      dataSchedulings: {
        userIdList: [],
        userType: '1', // 用户类型
        workPatternId: 0, // 工作模式id
        workPatternType: '1'
      }
    }
  },
  computed: {},
  mounted() {
    // const dom = document.getElementsByClassName('workGroupItem')
    document.body.addEventListener('click', (e) => {
      // console.log(e.target)
      if (this.showTree) {
        if (e.target.className !== 'workGroupItem' && e.target.className !== 'el-input__inner' && e.target.className !== 'el-icon-arrow-up') {
          this.showTree = false
        }
      }
      // // elTree
      // if (e.target.className !== 'workGroupItem' && e.target.className !== 'el-input__inner' && e.target.className !== 'el-icon-arrow-up') {
      //   this.showTree = false
      // }
    })
  },
  async created() {
    await this.render()
    this.areaList()
    this.workPatterns()
  },
  methods: {
    /**
     * 获取工作模式列表
     */
    async workPatterns() {
      const { data } = await workPatternsApi()
      console.log(data)
      this.workPatternList = data[1]
    },
    /**
     * 显示树形结构
     */
    async showTreeFn() {
      this.showTree = !this.showTree
      if (this.showTree) {
        this.iconArrow = 'el-icon-arrow-up'
      } else {
        this.iconArrow = 'el-icon-arrow-down'
      }
      // 如果树形控件没有展示, 直接return
      if (!this.showTree) return
    },
    /**
     * 获取机构列表树形数据
     */
    async areaList() {
      const { data } = await businessTreeApi()
      // console.log(JSON.parse(data))
      // const dataJson = JSON.parse(data)
      // this.dataTree = tranListToTreeData(dataJson, '')
      this.dataTree = JSON.parse(data)
      // console.log(this.dataTree)
    },
    /**
     * 点击所属机构下拉列表
     */
    handleNodeClick(data) {
      // console.log(data)
      this.formInline.organization = data.name
      this.showTree = false
    },
    selectChange(value) {
      this.formInline.workPattern = value
    },
    /**
     * 获取需要渲染的数据
     */
    async render() {
      const { data } = await getlistApi({
        page: this.formInline.page,
        pageSize: this.formInline.pageSize
      })
      // console.log(data.items)
      this.tableData = data.items
      this.pages = +data.pages
      this.counts = +data.counts
    },
    /**
     * 点击搜索按钮
     */
    async onSubmit() {
      const { data: agency } = await businessTreeApi()
      const arr1 = JSON.parse(agency)
      // console.log(arr1)
      arr1.forEach((ele) => {
        if (ele.name === this.formInline.organization && ele.name !== '') {
          // console.log(ele)
          this.formInline.agencyId = ele.id
        }
      })

      const { data: workP } = await workPatternsApi()
      const arr2 = workP[1]
      // console.log(arr2)
      arr2.forEach((ele) => {
        if (ele.name === this.formInline.workPattern && ele.name !== '') {
          this.formInline.workPatternId = ele.id
        }
      })
      this.loading = true
      const form = {} // 保存非空的对象
      for (const key in this.formInline) {
        if (key !== '') {
          // 只要对象属性值非空的
          form[key] = this.formInline[key]
        }
      }
      // console.log(form)
      const { data } = await getlistApi(form)
      // console.log(data)
      this.tableData = data.items
      this.pages = +data.pages
      this.counts = +data.counts
      this.loading = false
      // setTimeout(this.formInline = {}, 500)
    },
    reset() {
      // console.log('重置')
      this.$refs.form.resetFields()
      this.render()
      // this.formInline = {}
    },
    /**
     * 员工角色数据处理
     */
    formatter(row, column) {
      const obj = userType.userType.find((ele) => ele.userType === row.userType)
      return obj ? obj.userTypeName : '未知'
    },
    handleSizeChange(val) {
      this.formInline.pageSize = val
      this.render()
    },
    handleCurrentChange(val) {
      this.formInline.page = val
      this.render()
    },
    prevNextClick(i) {
      this.formInline.page += i
      this.render()
    },
    /**
     * 排班设置
     */
    scheduleSetting() {
      this.$router.push({
        name: 'workArrange-setting'
        // params: {
        //   参数名: '参数值'
        // }
      })
    },
    /**
     * 绑定排班
     */
    bindingScheduling() {
      this.dataSchedulings.userIdList = []

      if (this.handleSelection.length === 0) {
        this.$message.error('请选择调整人员')
        return
      }
      // 加入需要的数据
      this.handleSelection.forEach(ele => this.dataSchedulings.userIdList.push(ele.id))

      this.dialogVisible = true
      this.workPatterns()
      // console.log(this.radio)
      this.workPatternListRadio = this.workPatternList
    },
    async changeRadio(radio) {
      this.dataSchedulings.workPatternType = radio
      if (radio === '1') {
        this.workPatternListRadio = this.workPatternList
      } else {
        const { data } = await workPatternsApi()
        this.workPatternListRadio = data[2]
      }
    },
    async submit() {
      if (!this.dataSchedulings.workPatternId) {
        this.$message.error('请选择模式名称')
        return
      }
      console.log(this.dataSchedulings)
      console.log(this.workPattern)
      const res = await workSchedulingsApi(this.dataSchedulings)
      console.log(res)
      this.render()

      this.dialogVisible = false
      this.workPattern = ''
    },
    getValue(val) {
      // console.log(val)
      this.dataSchedulings.workPatternId = val
      console.log(this.workPattern)
    },
    /**
		 * 表格多选
		 */
    handleSelectionChange(val) {
      this.handleSelection = val
    }
  }
}
</script>

<style lang="scss" scoped>
.customer-list-box {
  padding: 24px 20px 0 20px;

  .search-form {
    background-color: #fff;
    width: 100%;
    height: 160px;
    padding: 20px 40px 0 40px;
    margin-bottom: 20px;
  }
  /deep/ .el-form-item__label {
    font-weight: 400 !important;
    width: 82px;
    height: 40px;
    text-align: center;
  }
  /deep/ .customer-list-box .search-form[data-v-a740cc26] {
    display: flex;
    justify-content: space-between;
  }
  /deep/ .el-form--inline .el-form-item {
    margin-right: 50px;
  }
  /deep/ .el-form--inline .el-form-item__content {
    width: 235px;
  }
  /deep/ .el-button {
    width: 90px;
    text-align: center;
  }
  /deep/ .el-button:focus,
  .el-button:hover {
    background-color: #ffeeeb;
    color: #e15536;
    border: #e15536;
  }
  /deep/ .el-button--primary {
    background-color: #e15536;
    border: none;
  }
  /deep/ .el-button--primary:focus,
  .el-button--primary:hover {
    background-color: #ffab98;
    color: #fff;
  }
  /deep/ .setting {
    width: 100px;
  }
  /deep/ .setting {
    width: 100px;
  }
  /deep/ .binding {
    width: 100px;
    background-color: #ffeeeb;
    color: #e1556b;
    border: #f3917c solid 1px;
  }
  /deep/ .binding:hover,
  .binding:focus {
    background-color: #ff6b4a;
    color: #fff;
    border: #ff6b4a solid 1px;
  }
}

.table {
  background-color: #fff;
  padding: 28px;
  height: 100%;
  /deep/ .el-table th > .cell {
    text-align: center;
  }
  /deep/ .el-table tbody .cell {
    text-align: center;
  }
  .workSchedules-shang {
    display: inline-block;
    width: 24px;
    height: 24px;
    background-color: #ffeeeb;
    color: #e15536;
    border: #e35f42 1px solid;
    border-radius: 12px;
  }
  .workSchedules-xiu {
    display: inline-block;
    width: 24px;
    height: 24px;
    background-color: #eff3f8;
    color: #20232a;
    border: #dadfe5 1px solid;
    border-radius: 12px;
  }

  .table-list {
    margin-top: 28px;
  }
  .pagination {
    width: 700px;
    margin: 20px auto 30px;
  }
  /deep/ .el-pager li.active {
    color: #e15536;
  }
}
/deep/ .el-radio__input.is-checked+.el-radio__label{
	color: #e15536;
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
/deep/ th.date{
  .cell{
    padding-left: 17px!important;
  }
}
.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.history-name {
  width: auto !important;
  /deep/ .el-input__inner {
    width: 293px;
  }
}

.workArrange-manage-people-setting {
  /deep/ .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }
  /deep/ .el-dialog__footer {
    padding-bottom: 40px;
  }
}

.work-type {
  /deep/ .el-form-item__content {
    margin-left: 40px !important;
  }
  /deep/ .el-radio:first-child {
    margin-right: 80px !important;
  }
  /deep/.el-radio__label {
    font-weight: bold;
    color: #333333;
  }
}

.dialog-footer {
  .el-button {
    width: 89px;
    height: 36px;
    padding: 10px 20px;
  }
}

.rest {
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}

.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
.transport-task /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

// 下拉框
/deep/ .el-select {
  width: 100%;
}

/*定义滚动条轨道 内阴影+圆角*/
/deep/ .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;
  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}
/deep/ .el-card{
  overflow: initial;
}
</style>

<style></style>
